﻿namespace EnergyGeneratorSimVisualizer.Views
{
    using System;
    using System.Collections.Generic;
    using System.Windows;
    using System.Windows.Controls;

    /// <summary>
    /// Interaction logic for ChartConfiguration.xaml
    /// </summary>
    public partial class ChartConfiguration : UserControl
    {
        public ChartConfiguration()
        {
            this.InitializeComponent();
        }

        public Action<IEnumerable<ChartDescription>> CheckedChangedHandler { get; set; }

        public void Update()
        {
            this.OnCheckedChanged(null, null);
        }

        private IEnumerable<ChartDescription> GetChartDescriptions()
        {
            var chartDescriptions = new List<ChartDescription>();

            foreach (CheckBox checkBox in this.CheckBoxList.Children)
            {
                if (!(bool)checkBox.IsChecked)
                {
                    continue;
                }

                chartDescriptions.Add(new ChartDescription
                    {
                        TitleContent = checkBox.Content,
                        ValuePath = (string)checkBox.Tag
                    });
            }

            return chartDescriptions;
        }

        private void OnCheckedChanged(object sender, RoutedEventArgs e)
        {
            if (this.CheckedChangedHandler != null)
            {
                var chartDescriptions = this.GetChartDescriptions();

                this.CheckedChangedHandler(chartDescriptions);
            }
        }
    }

}
